﻿using System;

using NUnit.Framework;

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class B001_AuthenticateTest {
		private static IWebDriver driver;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public static void TestInitialize() {
			driver = TestHelper.GetDriver();
		}

		[TestFixtureTearDown]
		public static void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void TestFailedLogOn() {
			driver.VisitURL("forum/account/logon");
			//driver.Navigate().GoToUrl(String.Format("{0}account/logon", TestHelper.SiteBaseURL));

			IWebElement emailBox = driver.FindElement(By.Name("EmailAddress"));
			Assert.IsNotNull(emailBox);
			emailBox.SendKeys("bogus@mvcforum.org");

			IWebElement passwordBox = driver.FindElement(By.Name("Password"));
			Assert.IsNotNull(passwordBox);
			passwordBox.SendKeys(Constants.Users.Admin.Password);

			IWebElement button = driver.FindElement(By.ClassName("default-submit-action"));
			Assert.IsNotNull(button);
			button.Click();

			Assert.AreEqual(String.Format("{0}forum/account/logon", TestConfiguration.SiteBaseURL), driver.Url.ToLower());
			// TODO:
		}

		[Test]
		public void TestLogOn() {
			driver.VisitURL("forum/account/logon");
			//driver.Navigate().GoToUrl(String.Format("{0}account/logon", TestHelper.SiteBaseURL));

			IWebElement emailBox = driver.FindElement(By.Name("EmailAddress"));
			Assert.IsNotNull(emailBox);
			emailBox.SendKeys(Constants.Users.Admin.Email);

			IWebElement passwordBox = driver.FindElement(By.Name("Password"));
			Assert.IsNotNull(passwordBox);
			passwordBox.SendKeys(Constants.Users.Admin.Password);

			IWebElement button = driver.FindElement(By.ClassName("default-submit-action"));
			Assert.IsNotNull(button);
			button.Click();

			driver.Wait(5);

			Assert.AreEqual(String.Format("{0}forum", TestConfiguration.SiteBaseURL), driver.Url.ToLower());
			var auth = driver.Manage().Cookies.GetCookieNamed(".ASPXAUTH");
			if (auth == null) {
				auth = driver.Manage().Cookies.GetCookieNamed(".AspNet.ApplicationCookie");
			}
			Assert.IsNotNull(auth);
		}
	}
}